﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace CIT.WMS.houseWork.Bll
{
    public class Bll_Inventory_Detail
    {
        public static bool QueryReelidStatus(string varReelid, string varInventoryCode, out string varMsg)
        {
            string sqlQuery = string.Format(@"
declare @ReelID nVarchar(50)--Reelid
declare @R_PartNumber nVarchar(50)--Reelid的料号
declare @R_HouseCode nVarchar(50)--Reelid的仓库
declare @R_ShelfID nVarchar(50)--Reelid的Shelfid
declare @R_Qty nVarchar(50)--Reelid的Qty
declare @InventoryCode nVarchar(50)--单据号
declare @I_HouseCode nVarchar(50)--单据号下的仓库
declare @I_Location nVarchar(50)--单据号下的库位
declare @I_Area nVarchar(50)--单据号下的库区
declare @I_PartNumber nVarchar(50) --单据号下的料号
set @ReelID='{0}'
set @InventoryCode='{1}'
if not exists(select *  from T_Bllb_StockInfo_tbsi where SerialNumber=@ReelID)
	begin
		select '0','请先注册料盘' return
	end
if not exists(select *  from T_Bllb_StockInfo_tbsi where SerialNumber=@ReelID and Lock_Flag=1)
	begin
		select '0','该Reelid无盘点计划' return
	end 
select @R_PartNumber=MaterialCode,@R_HouseCode=Storage_SN,@R_ShelfID=Location_SN,@R_Qty=QTY from dbo.T_Bllb_StockInfo_tbsi where SerialNumber=@ReelID
--校验是否存在于别的未完成的盘点单中
if exists(select *  from dbo.T_InventoryDetail_tid  as a inner join dbo.T_Inventory_ti as b on a.InventoryCode=b.InventoryCode where a.ReelId=@ReelID and b.Status<>'2' )
	begin
		--存在于别的盘点单中
		select '0','该Reelid无盘点计划' return
	end
--不存在于别的盘点单中 继续往下执行
--单据+料号-》detail表查询是否存在
if not exists (select *  from dbo.T_Inventory_Detail where InventoryCode=@InventoryCode and PN=@R_PartNumber)--第三种情况
	begin
		--不存在
		select @I_PartNumber=PN ,@I_HouseCode=HouseCode from dbo.T_Inventory_ti where InventoryCode=@InventoryCode
		--料号是否为空,不为空则停止执行
		if(@I_PartNumber='' or @I_PartNumber is null)
			begin
				if((@I_HouseCode=@R_HouseCode) or(@R_HouseCode='' or @R_HouseCode is null))--仓库（单据）==仓库/空(Reelid)?
				    begin
					  --插入T_InventoryDetail_tid  reelid、状态、已盘数、数量(0)、单据号、
						insert into dbo.T_InventoryDetail_tid (Status,CurrentQty,Creator,CreateTime,InventoryCode,ReelId,Qty)
						values(0,@R_Qty,'{2}',getDate(),@InventoryCode,@ReelID,0)
					  --插入T_Inventory_Detail 已盘数、料号、单据号
						--update dbo.T_Inventory_Detail set CurrentQty=convert(decimal,Convert(decimal,CurrentQty)+convert(decimal,@R_Qty)) where InventoryCode=@InventoryCode					  
                        insert into T_Inventory_Detail(PN,CurrentQty,InventoryCode) values(@R_PartNumber,convert(decimal,@R_Qty),@InventoryCode)
                      --更新T_Inventory_ti 盘点状态--》盘点中
						update dbo.T_Inventory_ti set Status='1' where InventoryCode=@InventoryCode
                      --更新库存表MdcDatVStorage Reelid对应的仓库
                        update T_Bllb_StockInfo_tbsi set Storage_SN=@I_HouseCode,Lock_Flag=1 where SerialNumber=@ReelID
						select '1','OK' return
                    end
				else
					begin
					  select '0','该Reelid无盘点计划' return
                    end                    
			end
		else
			begin
				select '0','该Reelid无盘点计划' return
			end
	end
else--第二种情况	
	begin
		--存在
		--根据单据号，找出单据号对应的仓库、库位、库区
		select @I_HouseCode=HouseCode,@I_Location=StorageLocation,@I_Area=StorageArea from dbo.T_Inventory_ti where InventoryCode=@InventoryCode
		if((@I_HouseCode=@R_HouseCode) or (@R_HouseCode='' or @R_HouseCode is NULL))--1.仓库（单据号）==仓库/空(Reelid)  ？
		  begin
		    if((@I_Location=@R_ShelfID)or(@I_Location=''or @I_Location is null)) --2.库位/空(单据号)==ShelfID（ReelID）  ？
			  begin
			  --插入T_InventoryDetail_tid  reelid、状态、数量、未盘数、单据号、
			    insert into dbo.T_InventoryDetail_tid (Status,CurrentQty,Creator,CreateTime,InventoryCode,ReelId,Qty)
			    values(0,@R_Qty,'{2}',getDate(),@InventoryCode,@ReelID,0)
			  --更新T_Inventory_Detail 已盘数
			    update dbo.T_Inventory_Detail set CurrentQty=convert(decimal,Convert(decimal,CurrentQty)+convert(decimal,@R_Qty)) where InventoryCode=@InventoryCode
			  --更新T_Inventory_ti 盘点状态--》盘点中
			    update dbo.T_Inventory_ti set Status='1' where InventoryCode=@InventoryCode
              --更新库存表MdcDatVStorage Reelid对应的仓库
              update T_Bllb_StockInfo_tbsi set Storage_SN=@I_HouseCode,Lock_Flag=1 where SerialNumber=@ReelID
			    select '1','OK' return
			  end
			else --end //2.--库位/空(单据号)==ShelfID（ReelID）  ？
			  begin
				 select '0','该Reelid无盘点计划' return
			  end
          end
        else--end //1.--仓库（单据号）==仓库/空(Reelid)  ？
		  begin
			 select '0','该Reelid无盘点计划' return	
          end		
	end", varReelid, varInventoryCode, Utils.uContext.UserNamek__BackingField);
            DataTable dt_Query = Utils.NMS.QueryDataTable(Utils.uContext, sqlQuery, new CIT.WMS.MESWcf.CmdParameter[0]);
            if (dt_Query.Rows[0][0].ToString() == "0")
            {
                varMsg = dt_Query.Rows[0][1].ToString();
                return false;
            }
            else
            {
                varMsg = dt_Query.Rows[0][1].ToString();
                return true;
            }
        }

        public static DataTable QueryBarCodeInfo(string strWhere)
        {
            string strSql = string.Format(@"select Storage_SN,Area_SN,Location_SN,MaterialCode,QTY,Lock_Flag,Enable,
In_Time,SerialNumber,Scrapt_Time,Status_Flag,Finally_Time,PLCode,DateCode,Reback_Flag  from dbo.T_Bllb_StockInfo_tbsi {0}", strWhere);
            DataTable dt_Query = Utils.NMS.QueryDataTable(Utils.uContext, strSql, new CIT.WMS.MESWcf.CmdParameter[0]);
            return dt_Query;
        }
    }
}
